package cn.iocoder.yudao.module.erp.dal.mysql.product;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.erp.controller.admin.pristock.vo.PriStockRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.relationship.vo.RelationshipPageReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.departquantity.DepartQuantityDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.relationship.RelationshipDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * ERP 产品 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ErpProductMapper extends BaseMapperX<ErpProductDO> {

    default PageResult<ErpProductDO> selectPage(ErpProductPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ErpProductDO>()
                .likeIfPresent(ErpProductDO::getName, reqVO.getName())
                .likeIfPresent(ErpProductDO::getFullPinyin, reqVO.getFullPinyin())
                .likeIfPresent(ErpProductDO::getFirstPinyin, reqVO.getFirstPinyin())
                .likeIfPresent(ErpProductDO::getWubiShortcode, reqVO.getWubiShortcode())
                .likeIfPresent(ErpProductDO::getManufacturer, reqVO.getManufacturer())
                .eqIfPresent(ErpProductDO::getIsFree, reqVO.getIsFree())
                .eqIfPresent(ErpProductDO::getIsDedicated, reqVO.getIsDedicated())
                .likeIfPresent(ErpProductDO::getRegistCertNum, reqVO.getRegistCertNum())
                .likeIfPresent(ErpProductDO::getMedicalInsurCode, reqVO.getMedicalInsurCode())
                .eqIfPresent(ErpProductDO::getId,reqVO.getId())
                .eqIfPresent(ErpProductDO::getCategoryId, reqVO.getCategoryId())
                .eqIfPresent(ErpProductDO::getUnitId, reqVO.getUnitId())
                .eqIfPresent(ErpProductDO::getIsQuantity, reqVO.getIsQuantity())
                .eqIfPresent(ErpProductDO::getDeleted, reqVO.getDeleted())
                .betweenIfPresent(ErpProductDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ErpProductDO::getDataId));
    }

   List<ErpProductDO>  selectPages(ErpProductPageReqVO epDo);

    List<ErpProductDO>  getProductPagsForRelation(ErpProductPageReqVO epDo);

    List<RelationshipDO>getRelationShip(List<Long> productId);

   int  total(ErpProductPageReqVO epDo);

    default Long selectCountByCategoryId(Long categoryId) {
        return selectCount(ErpProductDO::getCategoryId, categoryId);
    }

    default Long selectCountByUnitId(Long unitId) {
        return selectCount(ErpProductDO::getUnitId, unitId);
    }

    default List<ErpProductDO> selectListByStatus(Integer status) {
        return selectList(ErpProductDO::getStatus, status);
    }

    /**
     * 产品名称筛选框查询方法
     * @param status
     * @param like
     * @return
     */
    @Select("select id, name, full_pinyin, first_pinyin, wubi_shortcode, bar_code, category_id, unit_id, manufacturer, is_quantity, is_free, is_dedicated, regist_cert_num, medical_insur_code, standard, weight, purchase_price, sale_price, min_price, price_update_time, remark, status, creator, create_time, updater, update_time, deleted, tenant_id, category_name, unit_name,concat(name,'(规格：',standard,')') full_name  from erp_product where deleted=0 and status =#{status} and (name like #{like} or full_pinyin like #{like}  or wubi_shortcode like #{like}  or first_pinyin like #{like} ) limit 50;")
    List<ErpProductRespVO> selectLists(Integer status,String like);

    /**
     * 产品名称筛选框查询方法
     * @param status
     * @param like
     * @return
     */
    @Select("select id, name, full_pinyin, first_pinyin, wubi_shortcode, bar_code, category_id, unit_id, manufacturer, is_quantity, is_free, is_dedicated, regist_cert_num, medical_insur_code, standard, weight, purchase_price, sale_price, min_price, price_update_time, remark, status, creator, create_time, updater, update_time, deleted, tenant_id, category_name, unit_name,concat(name,'(规格：',standard,')') full_name  from erp_product where deleted=0 and status =#{status} and (name like #{like} or full_pinyin like #{like}  or wubi_shortcode like #{like}  or first_pinyin like #{like} ) LIMIT #{pageSize} OFFSET #{page};")
    List<ErpProductDO> getProductToget(Integer status,String like,Integer page,Integer pageSize);

    int selectTotalfrom(String like);

    @Select("<script>" +
            "select id, name, full_pinyin, first_pinyin, wubi_shortcode, standard from erp_product " +
            "where deleted=0 and status =#{status} " +
            "and (name like #{like} " +
            "or full_pinyin like #{like}" +
            "or wubi_shortcode like #{like} " +
            "or first_pinyin like #{like}) " +
            "<if test='ids != null and ids.length > 0'> " +
            "and id in " +
            "<foreach item='id' index='index' collection='ids' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach> " +
            "</if> " +
            "limit 50" +
            "</script>")
    List<ErpProductRespVO> selectListBySec(@Param("status")Integer status,@Param("like") String like, @Param("ids") Integer[] ids);

    default ErpProductDO selectById(Long id) {
        return selectOne(ErpProductDO::getDataId, id);
    }
    @Select("SELECT" +
            " data_id," +
            " id," +
            " full_pinyin," +
            " first_pinyin," +
            "wubi_shortcode," +
            "NAME," +
            "bar_code," +
            "category_id," +
            "unit_id," +
            "manufacturer," +
            "is_free," +
            "is_dedicated," +
            "regist_cert_num," +
            "medical_insur_code," +
            "STATUS," +
            "standard," +
            "remark," +
            "weight," +
            "purchase_price," +
            "sale_price," +
            "min_price," +
            "is_quantity," +
            "unit_name," +
            "category_name," +
            "start_time," +
            "end_time," +
            "create_time," +
            "update_time," +
            "creator," +
            "updater," +
            "deleted " +
            "FROM " +
            "erp_product " +
            "WHERE " +
            " deleted = 0 " +
            "AND id = #{id}" +
            "and start_time = (select max(start_time) from erp_product where deleted=0 and status = 0 and id =#{id})")
    ErpProductDO selectByIdStartTime(Long id);

    default ErpProductDO getQuantity(Long id) {
        return selectOne(ErpProductDO::getId, id);
    }
    @Update("update erp_product set deleted = #{isDelete} where data_id = #{id} ")
    void logicallyDeleteById(Long id,int isDelete);
    @Select(" select distinct product_id,quantity_count,allocation_status from erp_depart_quantity where product_id = #{ids}")
    PriStockRespVO selectPri(Long ids);
    @Select("select distinct product_id,quantity_count,task_start_time,task_end_time,allocation_status from erp_depart_quantity where product_id = #{ids}")
    DepartQuantityDO getDepart(Long id);

    int deleteRelationship(Long id);

    @Update("update erp_product set end_time = #{now} where data_id = #{dataId}")
    void updateProductPrice(Long dataId, LocalDateTime now);

}